package com.dabai.dbutils.popup.menu;

import android.content.Context;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.widget.PopupMenu;

/**
 * PopupMenu工具类
 *
 * @author BAI
 */
public class PopupMenuUtils {

    public interface OnItemListener {
        /**
         * 单击项目
         *
         * @param item 项
         */
        void clickItem(MenuItem item);

        /**
         * 消失
         */
        void onDismiss(PopupMenu menu);
    }

    public static void showPopupMenu(Context context, View view,int menu,OnItemListener onItemListener) {
        // 这里的view代表popupMenu需要依附的view
        PopupMenu popupMenu = new PopupMenu(context, view);
        // 获取布局文件
        popupMenu.getMenuInflater().inflate(menu, popupMenu.getMenu());
        popupMenu.show();
        // 通过上面这几行代码，就可以把控件显示出来了
        popupMenu.setOnMenuItemClickListener(item -> {
            onItemListener.clickItem(item);
            return true;
        });
        popupMenu.setOnDismissListener(onItemListener::onDismiss);
        popupMenu.show();

    }

}
